package com.yiwei.zhh.activity;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yiwei.zhh.R;
import com.yiwei.zhh.constant.Const;
import com.yiwei.zhh.util.PreferenceUtils;
import com.yiwei.zhh.view.ObservableScrollView;
import com.yiwei.zhh.view.V19FrameLayout;
import com.readystatesoftware.systembartint.SystemBarTintManager;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 2017/8/18.
 */

public class LevelRuleActivity extends BaseActivity implements ObservableScrollView.ScrollViewListener {
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.toolbar)
    FrameLayout toolbar;
    @BindView(R.id.img_level)
    ImageView imgLevel;
    @BindView(R.id.txt_tips)
    TextView txtTips;
    @BindView(R.id.img_star1)
    TextView imgStar1;
    @BindView(R.id.img_star2)
    TextView imgStar2;
    @BindView(R.id.img_star3)
    TextView imgStar3;
    @BindView(R.id.img_star4)
    TextView imgStar4;
    @BindView(R.id.img_star5)
    TextView imgStar5;
    @BindView(R.id.img_star6)
    TextView imgStar6;
    @BindView(R.id.txt_star1)
    TextView txtStar1;
    @BindView(R.id.txt_star2)
    TextView txtStar2;
    @BindView(R.id.txt_star3)
    TextView txtStar3;
    @BindView(R.id.txt_star4)
    TextView txtStar4;
    @BindView(R.id.txt_star5)
    TextView txtStar5;
    @BindView(R.id.txt_star6)
    TextView txtStar6;
    @BindView(R.id.scrollView)
    ObservableScrollView scrollView;
    @BindView(R.id.toolbar_sec)
    V19FrameLayout toolbarSec;
    @BindView(R.id.img_tips)
    ImageView imgTips;
    @BindView(R.id.btn_back_sec)
    ImageView btnBackSec;
    @BindView(R.id.btn_back_third)
    ImageView btnBackThird;
    @BindView(R.id.layout_top)
    RelativeLayout layoutTop;
    @BindView(R.id.img_bolang)
    ImageView imgBolang;
    private Unbinder unbinder;
    private float headerHeight;
    private float minHeaderHeight;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_level_rule);
        unbinder = ButterKnife.bind(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //透明导航栏
//            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            SystemBarTintManager tintManager = new SystemBarTintManager(this);
            tintManager.setStatusBarTintColor(Color.TRANSPARENT);
            tintManager.setNavigationBarTintColor(Color.parseColor("#000000"));
            tintManager.setStatusBarTintEnabled(true);
            tintManager.setNavigationBarTintEnabled(true);
        }
        imgBolang.post(new Runnable() {
            @Override
            public void run() {
                headerHeight = imgBolang.getTop();
            }
        });
        init();


//        initMeasure();
    }

    private void initMeasure() {
        headerHeight = getResources().getDimension(R.dimen.dp_300);
        minHeaderHeight = getResources().getDimension(R.dimen.abc_action_bar_default_height_material);
    }

    private void init() {
        int next_level_point_diff = PreferenceUtils.getInt(this, Const.KEY.LEVEL_POINT, 0);
        int level = PreferenceUtils.getInt(this, Const.KEY.LEVEL, 0);
        if (level == 6) {
            txtTips.setText("你已经是最高等级了，快去享受优惠吧");
        } else {
            txtTips.setText("还差" + next_level_point_diff + "积分升到LV" + (level + 1) + "，继续努力哦");
        }


        switch (level) {
            case 1:
                imgLevel.setImageResource(R.mipmap.dialog_lv1);
                imgStar1.setBackgroundResource(R.mipmap.img_hasstar);
                txtStar1.setTextColor(0xffc49d77);
                break;
            case 2:
                imgLevel.setImageResource(R.mipmap.dialog_lv2);
                imgStar1.setBackgroundResource(R.mipmap.img_hasstar);
                imgStar2.setBackgroundResource(R.mipmap.img_hasstar);
                txtStar1.setTextColor(0xffc49d77);
                txtStar2.setTextColor(0xffc49d77);
                break;
            case 3:
                imgLevel.setImageResource(R.mipmap.dialog_lv3);
                imgStar1.setBackgroundResource(R.mipmap.img_hasstar);
                imgStar2.setBackgroundResource(R.mipmap.img_hasstar);
                imgStar3.setBackgroundResource(R.mipmap.img_hasstar);
                txtStar1.setTextColor(0xffc49d77);
                txtStar2.setTextColor(0xffc49d77);
                txtStar3.setTextColor(0xffc49d77);

                break;
            case 4:
                imgLevel.setImageResource(R.mipmap.dialog_lv4);
                imgStar1.setBackgroundResource(R.mipmap.img_hasstar);
                imgStar2.setBackgroundResource(R.mipmap.img_hasstar);
                imgStar3.setBackgroundResource(R.mipmap.img_hasstar);
                imgStar4.setBackgroundResource(R.mipmap.img_hasstar);
                txtStar1.setTextColor(0xffc49d77);
                txtStar2.setTextColor(0xffc49d77);
                txtStar3.setTextColor(0xffc49d77);
                txtStar4.setTextColor(0xffc49d77);
                break;
            case 5:
                imgLevel.setImageResource(R.mipmap.dialog_lv5);
                imgStar1.setBackgroundResource(R.mipmap.img_hasstar);
                imgStar2.setBackgroundResource(R.mipmap.img_hasstar);
                imgStar3.setBackgroundResource(R.mipmap.img_hasstar);
                imgStar4.setBackgroundResource(R.mipmap.img_hasstar);
                imgStar5.setBackgroundResource(R.mipmap.img_hasstar);
                txtStar1.setTextColor(0xffc49d77);
                txtStar2.setTextColor(0xffc49d77);
                txtStar3.setTextColor(0xffc49d77);
                txtStar4.setTextColor(0xffc49d77);
                txtStar5.setTextColor(0xffc49d77);
                break;
            case 6:
                imgLevel.setImageResource(R.mipmap.dialog_lv6);
                imgStar1.setBackgroundResource(R.mipmap.img_hasstar);
                imgStar2.setBackgroundResource(R.mipmap.img_hasstar);
                imgStar3.setBackgroundResource(R.mipmap.img_hasstar);
                imgStar4.setBackgroundResource(R.mipmap.img_hasstar);
                imgStar5.setBackgroundResource(R.mipmap.img_hasstar);
                imgStar6.setBackgroundResource(R.mipmap.img_hasstar);
                txtStar1.setTextColor(0xffc49d77);
                txtStar2.setTextColor(0xffc49d77);
                txtStar3.setTextColor(0xffc49d77);
                txtStar4.setTextColor(0xffc49d77);
                txtStar5.setTextColor(0xffc49d77);
                txtStar6.setTextColor(0xffc49d77);
                break;
        }


        scrollView.setScrollViewListener(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
        unbinder = null;
    }

    @Override
    public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) {
        //Y轴偏移量
        float scrollY = scrollView.getScrollY();
//        e3483d
        //变化率
//        float headerBarOffsetY = headerHeight - minHeaderHeight;//Toolbar与header高度的差值
//        float offset = 1 - Math.max((headerBarOffsetY - scrollY) / headerBarOffsetY, 0f);


        //Toolbar背景色透明度
        if (scrollY >= headerHeight) {
//            leftImgFirst.setVisibility(View.VISIBLE);
//            if (toolbar.getVisibility() == View.GONE) {
//                return;
//            }
            btnBackThird.setVisibility(View.GONE);
            btnBackSec.setVisibility(View.VISIBLE);
        } else {
//            leftImgFirst.setVisibility(View.GONE);
//            if (toolbar.getVisibility() == View.VISIBLE) {
//                toolbar.setBackgroundColor(Color.argb((int) (offset * 255), 227, 72, 61));
//                return;
//            }
            btnBackThird.setVisibility(View.VISIBLE);
            btnBackSec.setVisibility(View.GONE);

        }

    }


    @OnClick({R.id.btn_back_sec, R.id.btn_back_third})
    public void onViewClicked(View view) {
        switch (view.getId()) {
//            case R.id.left_img:
//                finish();
//                break;
            case R.id.btn_back_third:
            case R.id.btn_back_sec:
                finish();
                break;
        }
    }
}
